﻿using AutoMapper;
using MediatR;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Infra.Repository;
using ZGLXM.Writh.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Writh.Api.Applicantion.Handler.RBAC
{
    public class RoleDeleteCommandHandler : IRequestHandler<RoleDeleteCommand, ApiResult>
    {
        private readonly IRoleRepository roleRepository;
        private readonly IMapper mapper;

        public RoleDeleteCommandHandler(IRoleRepository roleRepository, IMapper mapper)
        {
            this.roleRepository = roleRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> Handle(RoleDeleteCommand request, CancellationToken cancellationToken)
        {
            var role = await roleRepository.GetAsync(x => x.RoleId == request.RoleId);
            if (role == null)
            {
                return ApiResult.Fail("角色不存在");
            }
            role.IsDeleted = true;
            var res = await roleRepository.UpdateAsync(role);
            if (res > 0)
            {
                return ApiResult.OK("删除成功");
            }
            else
            {
                return ApiResult.Fail("删除失败");
            }
        }
    }
}
